﻿using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
using System.Text;
namespace ZXMOBIWEB.Proc
{
    /// <summary>
    /// GetCalendar 的摘要说明
    /// </summary>
    public class GetCalendar : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string ym = context.Request["YMType"];
            context.Response.Write(returnJsonM(ym));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string returnJsonM(string type)
        {
            StringBuilder sb = new StringBuilder();
            if (type != "")
            {
                string stry = type.Substring(0,4);
                string strm = type.Substring(4);
                sb.Append(type+getDate(Convert.ToInt32(stry), Convert.ToInt32(strm))); 
            }
            else
            {
                for (int i = 2010; i < 2020; i++)
                {
                    for (int j = 1; j < 13; j++)
                    {
                        sb.Append(i.ToString()+j.ToString("00")+getDate(i,j)+";"); 
                    }
                }
            }
            return sb.ToString();
        }

        //获取当月首日星期几、月总天数
        public string getDate(int y, int m)
        {
            int dates = DateTime.DaysInMonth(y, m);
            DateTime tmd = new DateTime(y, m, 1);
            int first = GetWeekDays(tmd);
            return  first.ToString() + dates.ToString();
        }

        //返回星期几
        public int GetWeekDays(DateTime dt)
        {
            int y = dt.Year;
            int d = dt.DayOfYear;
            decimal y4 = (y - 1) / 4;
            decimal y100 = (y - 1) / 100;
            decimal y400 = (y - 1) / 400;
            decimal w = (y - 1) + Math.Floor(y4) - Math.Floor(y100) + Math.Floor(y400) + d;
            return Convert.ToInt32(w) % 7;
        }
    }
}